package com.grj.boot.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.json.UTF8StreamJsonParser;
import com.grj.boot.domain.Shop;
import com.grj.boot.domain.Word;
import com.grj.boot.service.ShopService;
import com.grj.boot.util.ExcelUtil;
import com.grj.boot.util.TListener;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private ShopService shopService;

    final CsvReader reader = CsvUtil.getReader();


    @GetMapping
    public List<Shop> user(){
        return shopService.list();
    }

    @GetMapping("i")
    public String input() throws Exception {
        InputStream in = Files.newInputStream(Paths.get("C:\\Users\\ganru\\Downloads\\3.xlsx"));
        EasyExcel.read(in, Shop.class, new TListener(shopService)).sheet().doRead();
        return "导入订单列表成功";
    }

    @GetMapping("/{s}")
    public void  s(@PathVariable String s){
        LambdaQueryWrapper<Shop> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shop::getKeyword, s);
        List<Shop> list = shopService.list(queryWrapper);
        list.forEach(System.out::println);
    }

    @GetMapping("l")
    public void l() throws IOException {
        CsvReader reader = CsvUtil.getReader();
        CsvData data = reader.read(FileUtil.file("C:\\Users\\ganru\\Desktop\\马驰-数据需求2024年8月8日.csv"));
        List<Word> wordList = data.getRows().stream().map(i -> {
            Word word = new Word();
            word.setMarket(i.get(0));
            word.setKeyword(i.get(1));
            word.setSearchTime(i.get(2));
            return word;
        }).collect(Collectors.toList());
        wordList.remove(0);
        LambdaQueryWrapper<Shop> shopLambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Word word: wordList) {
            shopLambdaQueryWrapper.eq(Shop::getKeyword,word.getKeyword());
            shopLambdaQueryWrapper.or();
        }
        List<Shop> shopList = shopService.list(shopLambdaQueryWrapper);
        wordList.forEach(word->{
            for (Shop shop : shopList) {
                if(word.getKeyword().equals(shop.getKeyword())){
                    word.setRank(shop.getRanks());
                    continue;
                }
            }
        });
        wordList.forEach(System.out::println);
        ExcelUtil<Word> wordExcelUtil = new ExcelUtil<>();
        wordExcelUtil.export("ranks.xlsx", wordList);
    }




}
